package com.whmall.service.crm.provider;

import cn.hutool.core.collection.CollUtil;
import com.whmall.component.entity.dto.PageReqDTO;
import com.whmall.component.entity.dto.PageRspDTO;
import com.whmall.service.crm.api.SupplierChemicalRemarkRecordApi;
import com.whmall.component.entity.dto.CompanyKeyFieldDTO;
import com.whmall.service.crm.entity.dto.companyuser.CompanyUserKeyFieldRspDTO;
import com.whmall.service.crm.entity.dto.supplierchemical.SupplierChemicalRemarkRecordImportantFieldsRspDTO;
import com.whmall.service.crm.entity.dto.supplierchemical.SupplierChemicalRemarkRecordReqDTO;
import com.whmall.service.crm.entity.dto.supplierchemical.SupplierChemicalRemarkRecordRspDTO;
import com.whmall.service.crm.module.company.service.CompanyService;
import com.whmall.service.crm.module.companyuser.CompanyUserService;
import com.whmall.service.crm.module.supplierchemical.service.SupplierChemicalRemarkRecordService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SupplierChemicalRemarkRecordApi.class)
public class SupplierChemicalRemarkRecordProvider implements SupplierChemicalRemarkRecordApi {

    @Autowired
    private SupplierChemicalRemarkRecordService supplierChemicalRemarkRecordService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private CompanyUserService companyUserService;


    @Override
    public Map<Long, SupplierChemicalRemarkRecordImportantFieldsRspDTO> queryLatest(Long chemicalId, List<Long> companyIds) {
        return supplierChemicalRemarkRecordService.queryLatest(chemicalId, companyIds);
    }

    @Override
    public List<SupplierChemicalRemarkRecordImportantFieldsRspDTO> queryLatest(List<Long> chemicalIds, List<Long> companyIds) {
        return supplierChemicalRemarkRecordService.queryLatest(chemicalIds, companyIds);
    }

    @Override
    public PageRspDTO<SupplierChemicalRemarkRecordRspDTO> queryByChemicalId(Long chemicalId, PageReqDTO pageReqDTO) {

        PageRspDTO<SupplierChemicalRemarkRecordRspDTO> result =
                supplierChemicalRemarkRecordService.queryByChemicalId(chemicalId, pageReqDTO);

        List<SupplierChemicalRemarkRecordRspDTO> supplierChemicalRemarkRecordRspDTOS = result.getRecords();
        if (CollUtil.isEmpty(supplierChemicalRemarkRecordRspDTOS)){
            return result;
        }
        List<Long> companyIds = new ArrayList<>();
        List<Long> userIds = new ArrayList<>();
        supplierChemicalRemarkRecordRspDTOS.forEach(e -> {
            if (e.getCompanyId() != 0){
                companyIds.add(e.getCompanyId());
            }
            if (e.getUserId() != 0){
                userIds.add(e.getUserId());
            }
        });
        Map<Long, CompanyKeyFieldDTO> companyRspDTOMap = new HashMap<>();
        if (CollUtil.isNotEmpty(companyIds)){
            companyRspDTOMap = companyService.listByIds(companyIds);
        }
        Map<Long, CompanyUserKeyFieldRspDTO> companyUsers = new HashMap<>();
        if (CollUtil.isNotEmpty(userIds)){
            companyUsers = companyUserService.listByIds(userIds);
        }
        for (SupplierChemicalRemarkRecordRspDTO remark : supplierChemicalRemarkRecordRspDTOS) {
            if (companyRspDTOMap.containsKey(remark.getCompanyId())){
                remark.setCompanyName(companyRspDTOMap.get(remark.getCompanyId()).getName());
            }
            if (companyUsers.containsKey(remark.getUserId())){
                remark.setUserName(companyUsers.get(remark.getUserId()).getName());
                remark.setUserEmail(companyUsers.get(remark.getUserId()).getEmail());
                remark.setUserMobile(companyUsers.get(remark.getUserId()).getMobile());
            }
        }
        return result;
    }

    @Override
    public void add(SupplierChemicalRemarkRecordReqDTO reqDTO) {
        supplierChemicalRemarkRecordService.add(reqDTO);
    }
}
